package com.eldenring.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.eldenring.blockhandler.UserBlockHandler;
import com.eldenring.domain.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope // 实时刷新远程配置
public class UserCtrl {

    @Value("${server.port}")
    private String port;

    @GetMapping("/getUserById/{id}")
    @SentinelResource(value="getUserById",
                      blockHandler = "userExceptionHandler",
                      blockHandlerClass = UserBlockHandler.class,
                      fallback = "userFallBack")
    public User getUserById(@PathVariable Long id){

        int a = 1/0;
        return User.builder().id(id).name("天权凝光").intro("富甲一方" + port).build();
    }

    public static User userFallBack(Long id, Throwable throwable){

        return User.builder().id(404L).name("系统正在维护升级").intro("请稍后再试").build();
    }
}
